package com.jp.knowledge.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;

import com.google.gson.JsonObject;
import com.jiayen.util.ToasUtil;
import com.jp.knowledge.R;
import com.jp.knowledge.http.HttpHelper;
import com.jp.knowledge.model.IModel;

/**
 * Created by 528787067@qq.com on 2017/1/14.
 */
public class TalkBottomBarReplyView extends TalkBottomBarView {

    private int replyPosition;

    public TalkBottomBarReplyView(Context context) {
        this(context, null);
    }

    public TalkBottomBarReplyView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TalkBottomBarReplyView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        replyPosition = -1;
//        editTextBtn.setText("话题等你来畅谈...");
        sendRemindText.setVisibility(View.GONE);
//        sendBtn.setTextColor(context.getResources().getColor(R.color.font_black_think));
        sendBtn.setClickable(true);
//        editContent.removeTextChangedListener(textWatcher);
        setContentViewStyle(1);

        findViewById(R.id.icon_share_view).setVisibility(View.GONE);
    }

    @Override
    public void updateView() {
        if(replyBottomBarCallback == null || replyBottomBarCallback.setIsPraise() == 0) {
            iconPraiseImg.setImageResource(R.mipmap.like_a);
            iconPraiseText.setTextColor(context.getResources().getColor(R.color.font_black_think));
            iconPraiseText.setText(0 + "");
        } else {
            iconPraiseImg.setImageResource(R.mipmap.soliddz);
            iconPraiseText.setTextColor(context.getResources().getColor(R.color.font_orign));
            iconPraiseText.setText(replyBottomBarCallback.setPraiseNum() + "");
        }
    }

    public void setReplyPosition(int replyPosition){
        this.replyPosition = replyPosition;
    }
    public void showSoftAndEdit(){
        String toUserName = "";
        if(replyBottomBarCallback != null)
            toUserName = replyBottomBarCallback.setToUserName(replyPosition);
        if(toUserName == null)
            toUserName = "";
        editContent.setHint("回复" + toUserName);
        super.showSoftAndEdit();
    }
    public void hideSoftAndEdit(boolean flag){
        this.replyPosition = -1;
        super.hideSoftAndEdit(flag);
    }

    /* 点赞 */
    @Override
    public void praise() {
        if(replyBottomBarCallback == null)
            return;
        if (gotoLogin()) {
            return;
        } else if (replyBottomBarCallback.setIsPraise() == 1) {
            ToasUtil.toast(context, "抱歉，请勿重复点赞");
        } else{
            PraiseParamsBean praiseParamsBean = replyBottomBarCallback.addPraiseParamsBean();
            JsonObject param = new JsonObject();
            param.addProperty("token", HttpHelper.token);
            param.addProperty("topicId", praiseParamsBean.getTopicId());
            param.addProperty("infoId", praiseParamsBean.getInfoId());
            param.addProperty("type", infoDetailData.getLinkType());
            param.addProperty("commentId", praiseParamsBean.getCommentId());
            HttpHelper.getInstance(context).topicPraise(param, PRAISE_CODE, this);
        }
    }

    /* 提交评论 */
    @Override
    protected void commitComment() {
        if(editContent.getText().toString().trim().length() == 0){
            ToasUtil.toast(context, "请输入有效内容后再提交");
            return;
        } else if(showSwitchOrganizeDialog() || replyBottomBarCallback == null){
            hideSoftAndEdit(false);
            return;
        }
        ReplyParamsBean replyParamsBean = replyBottomBarCallback.addReplyParamsBean(replyPosition);
        JsonObject param = new JsonObject();
        param.addProperty("token", HttpHelper.token);
        param.addProperty("infoId", replyParamsBean.getInfoId());
        param.addProperty("topicId", replyParamsBean.getTopicId());
        param.addProperty("commentId", replyParamsBean.getCommentId());
        param.addProperty("toUid", replyParamsBean.getToUid());
        param.addProperty("toCommentId", replyParamsBean.getToCommentId());
        param.addProperty("content", editContent.getText().toString());
        param.addProperty("type",infoDetailData.getInfoData().getType());
        HttpHelper.getInstance(context).replyTopic(param,
                replyPosition >= 0 ? replyPosition*10+COMMIT_COMMENT_CODE : COMMIT_COMMENT_CODE, this);
        hideSoftAndEdit(true);
        loadingDialog.show();
    }

    @Override
    public void onError(int requestType) {
        if(requestType%10 == COMMIT_COMMENT_CODE){
            ToasUtil.toast(context, "抱歉，评论失败，请稍后重试");
            onCompleted(requestType);
        } else {
            super.onError(requestType);
        }
    }

    @Override
    public void onNext(IModel data, int requestType) {
        if(data.getErrcode() != 0){
            onError(requestType);
            return;
        }
        if(requestType%10 == COMMIT_COMMENT_CODE) {
            editContent.setText("");
            ToasUtil.toast(context, "评论成功");
        }
        if (this.replyBottomBarCallback != null)
            this.replyBottomBarCallback.httpEventFinish(data, requestType);
    }


    private ReplyBottomBarCallback replyBottomBarCallback;

    public void setReplyBottomBarCallback(ReplyBottomBarCallback replyBottomBarCallback){
        this.replyBottomBarCallback = replyBottomBarCallback;
        updateView();
    }

    public interface ReplyBottomBarCallback{
        PraiseParamsBean addPraiseParamsBean();
        ReplyParamsBean addReplyParamsBean(int replyPosition);
        String setToUserName(int replyPosition);
        int setIsPraise();
        int setPraiseNum();
        void httpEventFinish(IModel data, int requestCode);
    }

    public static class PraiseParamsBean{
        private String topicId;
        private String commentId;
        private String infoId;
        public String getInfoId() {
            return infoId;
        }
        public void setInfoId(String infoId) {
            this.infoId = infoId;
        }
        public String getTopicId() {
            return topicId;
        }
        public void setTopicId(String topicId) {
            this.topicId = topicId;
        }
        public String getCommentId() {
            return commentId;
        }
        public void setCommentId(String commentId) {
            this.commentId = commentId;
        }
    }

    public static class ReplyParamsBean{
        private String infoId;
        private String topicId;
        private String commentId;
        private String toUid;
        private String toCommentId;
        public String getInfoId() {
            return infoId;
        }
        public void setInfoId(String infoId) {
            this.infoId = infoId;
        }
        public String getTopicId() {
            return topicId;
        }
        public void setTopicId(String topicId) {
            this.topicId = topicId;
        }
        public String getCommentId() {
            return commentId;
        }
        public void setCommentId(String commentId) {
            this.commentId = commentId;
        }
        public String getToUid() {
            return toUid;
        }
        public void setToUid(String toUid) {
            this.toUid = toUid;
        }
        public String getToCommentId() {
            return toCommentId;
        }
        public void setToCommentId(String toCommentId) {
            this.toCommentId = toCommentId;
        }
    }
}
